package com.warren.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.warren.annotation.ControllerEndpoint;
import com.warren.response.Result;
import com.warren.system.entity.Menu;
import com.warren.system.service.MenuService;
import com.warren.system.vo.MenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author warren
 * @since 2021-03-21
 */
@Api(value = "菜单表", tags = "菜单相关操作")
@RestController
@RequestMapping("/menus")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation(value = "查询所有菜单列表",notes = "查询所有菜单列表")
    @GetMapping("/getMenus")
    public Result getMenu(){
        List<Menu> menus = menuService.getAll();
        menus.sort(Menu.order());
        return Result.ok().data("menus",menus);
    }

    @ApiOperation(value = "查询菜单列表",notes = "查询菜单列表")
    @PostMapping("/getMenus")
    public Result getMenus(@RequestBody MenuVO menuVO){
        try {
            QueryWrapper<Menu> wrapper = getWrapper(menuVO);
            List<Menu> menus = menuService.list(wrapper);
            return Result.ok().data("menus",menuService.permsTreeBuild(menus));
        } catch (Exception exception) {
           return Result.error().message("没有查询到");
        }
    }

    @ApiOperation(value = "查询下拉菜单列表")
    @GetMapping("/getMenuSelect")
    public Result getMenuSelect(){
        List<Menu> menus = menuService.getAll();
        return Result.ok().data("menus",menuService.menuTreeBuild(menus));
    }

    @ApiOperation(value = "删除菜单")
    @ControllerEndpoint(exceptionMessage = "删除菜单失败",operation = "删除菜单")
    @DeleteMapping("/delMenu")
    public Result delMenu(Long menuId){
        menuService.delMenu(menuId);
        return Result.ok().message("删除成功");
    }

    @ControllerEndpoint(exceptionMessage = "修改菜单失败",operation = "修改菜单")
    @ApiOperation(value = "修改菜单")
    @PutMapping("/updateMenu")
    public Result updateMenu(@RequestBody Menu menu){
        menuService.updateMenu(menu);
        return Result.ok().message("更新成功");
    }

    @ControllerEndpoint(exceptionMessage = "添加菜单失败",operation = "添加菜单")
    @ApiOperation(value = "添加菜单")
    @PostMapping("/addMenu")
    public Result addMenu(@RequestBody Menu menu){
        menuService.add(menu);
        return Result.ok().message("添加菜单成功");
    }

    @ApiOperation(value = "通过用户id查询菜单列表")
    @GetMapping("/getMenusByAdminId")
    public Result getMenusByAdminId(){
        List<Menu> menus = menuService.getMenusByAdminId();
        return Result.ok().data("menus",menuService.menuTreeBuild(menus));
    }



    private QueryWrapper<Menu> getWrapper(MenuVO menuVO){
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        if (menuVO != null){
            //查询每个条件
            if (!ObjectUtils.isEmpty(menuVO.getMenuName())){
                queryWrapper.like("menu_name",menuVO.getMenuName());
            }

            if (!ObjectUtils.isEmpty(menuVO.getAvailable())){
                queryWrapper.eq("available",menuVO.getAvailable());
            }

            if (!ObjectUtils.isEmpty(menuVO.getType())){
                queryWrapper.eq("type",menuVO.getType());
            }
        }
        return queryWrapper;
    }
}

